//
//  Control.m
//  NXTRemoteController
//
//  Created by ; on 14.05.12.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "Control.h"


@implementation Control


- (id)initWithFrame:(CGRect)frame andRobot:(Robot *) r {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		robot = r;
		
		//background image
		UIColor * background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"view.png"]];
		self.backgroundColor = background;
		[background release];
		
		//add Buttons
		UIButton * forward = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[forward addTarget:self action:@selector(moveForward:) forControlEvents:UIControlEventTouchDown];
		[forward addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
		[forward setTitle:@"" forState:UIControlStateNormal];
		[forward setImage:[UIImage imageNamed:@"forward.png"] forState:UIControlStateNormal];
		forward.frame = CGRectMake(324, 168, 120, 120);
		[self addSubview:forward];
		
		UIButton * backward = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[backward addTarget:self action:@selector(moveBackward:) forControlEvents:UIControlEventTouchDown];
		[backward addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
		[backward setTitle:@"" forState:UIControlStateNormal];
		[backward setImage:[UIImage imageNamed:@"backward.png"] forState:UIControlStateNormal];
		backward.frame = CGRectMake(324, 456, 120, 120);
		[self addSubview:backward];
		
		UIButton * left = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[left addTarget:self action:@selector(moveLeft:) forControlEvents:UIControlEventTouchDown];
		[left addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];		
		[left setTitle:@"" forState:UIControlStateNormal];
		[left setImage:[UIImage imageNamed:@"left.png"] forState:UIControlStateNormal];
		left.frame = CGRectMake(166, 314, 120, 120);
		[self addSubview:left];
		
		UIButton * right = [UIButton buttonWithType:UIButtonTypeRoundedRect];
		[right addTarget:self action:@selector(moveRight:) forControlEvents:UIControlEventTouchDown];
		[right addTarget:self action:@selector(stop:) forControlEvents:UIControlEventTouchUpInside];
		[right setTitle:@"" forState:UIControlStateNormal];
		[right setImage:[UIImage imageNamed:@"right.png"] forState:UIControlStateNormal];
		right.frame = CGRectMake(482, 314, 120, 120);
		[self addSubview:right];
		
		//add slider
		speedSlider = [[UISlider alloc] initWithFrame:CGRectMake(196, 64, 384, 30)];
		speedSlider.minimumValue = 0;
		speedSlider.maximumValue = 100;
		speedSlider.value = 76;
		[speedSlider addTarget:self action:@selector(sliderMoved:) forControlEvents:UIControlEventValueChanged];
		[self addSubview:speedSlider];
		
		//add info label
		UILabel * spLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 64, 80, 30)];
		spLabel.text = @"Скорость";
		spLabel.textColor = [UIColor darkGrayColor];
		spLabel.backgroundColor = [UIColor clearColor];
		[self addSubview:spLabel];
		
		//add speed
		speedLabel = [[UILabel alloc] initWithFrame:CGRectMake(614, 64, 50, 30)];
		speedLabel.text = @"76";
		speedLabel.textColor = [UIColor darkGrayColor];
		speedLabel.backgroundColor = [UIColor clearColor];		
		[self addSubview:speedLabel];
		
		//speed
		speed = 76;
		
	}
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
	NSLog(@"deallocControl");
    [super dealloc];	
}

- (IBAction) sliderMoved:(id) sender {
	speedLabel.text = [[NSString alloc] initWithFormat:@"%d", (int)[speedSlider value]];
	speed = 50 + (int)[speedSlider value] / 2;
}

- (IBAction) moveForward:(id) sender {
	NSLog(@"forward");
	[robot setSpeed:speed];
	[robot moveForward];
}

- (IBAction) moveBackward:(id) sender {	
	NSLog(@"backward");
	[robot setSpeed:speed];
	[robot moveBackward];
}

- (IBAction) moveLeft:(id) sender {
	NSLog(@"left");
	[robot setSpeed:speed];
	[robot moveLeft];
}

- (IBAction) moveRight:(id) sender {
	NSLog(@"right");
	[robot setSpeed:speed];
	[robot moveRight];
}
- (IBAction) stop:(id) sender {
	NSLog(@"stop");
	[robot stop];
}

@end
